package com.sr.vo.req;

import com.sr.pojo.WResources;
import com.sr.util.ToolUtil;
import com.sr.vo.BaseForm;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;
import java.util.Date;

/**
 * @author xiaochi
 * @date 2022/4/27 18:39
 * @desc ResourcesReqVo
 */
@Data
@ApiModel("资源实体")
@EqualsAndHashCode(callSuper = true)
public class ResourcesReqVo extends BaseForm<WResources> {

    @ApiModelProperty("资源id,更新时必须")
    @Length(min = 19, max = 19, message = "id不正确",groups = {Create.class,Update.class})
    @NotBlank(message = "id不能为空", groups = Update.class)
    private String id;

    @ApiModelProperty("资源名称长度不能超过32位")
    @NotBlank(message = "资源名称不能为空",groups = {Create.class,Update.class})
    @Length(min = 0, max = 32, message = "资源名称长度为 1 ~ 32 位",groups = {Create.class,Update.class})
    private String name;

    @ApiModelProperty("资源描述长度不能超过255个字符")
    @Length(max = 255, message = "资源描述长度为 0 ~ 255 位",groups = {Create.class,Update.class})
    private String desc;

    @ApiModelProperty("资源内容不能超过255字符")
    @NotBlank(message = "资源内容不能为空",groups = {Create.class,Update.class})
    @Length(max = 255, message = "资源内容长度为 0 ~ 255 位",groups = {Create.class,Update.class})
    private String content;

    @ApiModelProperty("资源类型id")
    @NotBlank(message = "请选择资源类型",groups = {Create.class,Update.class})
    @Size(min = 19,max = 19,message = "类型id不正确",groups = {Create.class,Update.class})
    private String typeId;

    @ApiModelProperty("资源状态")
    @Range(min = 0,max = 3,message = "状态：0删除，1正常，2未发布，3审核中",groups = {Select.class,Update.class})
    private Integer isDel;

    @Override
    public WResources buildEntity() {
        WResources wResources = new WResources();
        BeanUtils.copyProperties(this, wResources);
        Date date = new Date();
        if (StringUtils.isEmpty(wResources.getId())) {
            wResources.setId(ToolUtil.nextId());
            wResources.setCreateTime(date);
            wResources.setUpdateTime(date);
        } else {
            wResources.setUpdateTime(date);
        }
        return wResources;
    }
}
